package com.jia.coupons.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.coupons.entity.Voucher;
import com.jia.coupons.mapper.VoucherMapper;
import com.jia.coupons.service.VoucherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * (Voucher)表服务实现类
 *
 * @author makejava
 * @since 2024-10-05 21:18:11
 */
@Service("voucherService")
public class VoucherServiceImpl extends ServiceImpl<VoucherMapper, Voucher> implements VoucherService {

    @Resource
    private VoucherMapper voucherMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Voucher queryById(Long id) {
        return this.voucherMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param voucher 筛选条件
     * @param page    分页对象
     * @return 查询结果
     */
    @Override
    public Page<Voucher> queryByPage(Voucher voucher, Page page) {
        QueryWrapper<Voucher> wrapper = new QueryWrapper<>();
        wrapper.eq(!Objects.isNull(voucher.getShopId()), "shop_id", voucher.getShopId());
        return this.baseMapper.selectPage(page, wrapper);
    }

    /**
     * 新增数据
     *
     * @param voucher 实例对象
     * @return 实例对象
     */
    @Override
    public Voucher insert(Voucher voucher) {
        this.voucherMapper.insert(voucher);
        return voucher;
    }

    /**
     * 修改数据
     *
     * @param voucher 实例对象
     * @return 实例对象
     */
    @Override
    public Voucher update(Voucher voucher) {
        this.voucherMapper.updateById(voucher);
        return this.queryById(voucher.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.voucherMapper.deleteById(id) > 0;
    }
}
